package com.luoye.lj.platform.service.impl;

import com.luoye.lj.platform.constant.ServiceExceptionConstants;
import com.luoye.lj.platform.dao.IRoleServiceDao;
import com.luoye.lj.platform.model.domain.RoleCollection;
import com.luoye.lj.platform.model.domain.RoleInfo;
import com.luoye.lj.platform.model.domain.RoleInfoSearch;
import com.luoye.lj.platform.model.domain.RoleSaveInfo;
import com.luoye.lj.platform.model.enums.DeleteMark;
import com.luoye.lj.platform.model.exception.ServiceException;
import com.luoye.lj.platform.service.IRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class RoleServiceImpl implements IRoleService {
    private static Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Autowired
    private IRoleServiceDao dao;

    @Override
    @Transactional
    public boolean addRole(RoleSaveInfo role) throws ServiceException {
        logger.info("role->"+role);
        if(StringUtils.isEmpty(role.getDescription()) ){
            throw ServiceExceptionConstants.ROLE_NOT_DESCRIPTION;
        }
        if(StringUtils.isEmpty(role.getRolename())){
            throw ServiceExceptionConstants.ROLE_NOT_NULLRNAME;
        }
        if(null == role.getAuths() || 0 >= role.getAuths().size()){
            throw ServiceExceptionConstants.ROLE_NOT_NULLRRA;
        }
        if(role.getRoletype()<0){
            throw ServiceExceptionConstants.ROLE_NOT_NULLRTYPE;
        }
        RoleInfo roleInfo = dao.getRoleByname(role.getRolename());
        if (roleInfo == null) {
            RoleInfo roleInfo1 = new RoleInfo(role.getId(),role.getRolename(),role.getDescription(),role.getRoletype());
            dao.addRole(roleInfo1);
            role.setId(roleInfo1.getId());
            dao.addRra(role);
            return true;
        } else if (dao.getDelmark(roleInfo.getId()) == DeleteMark.DELETED.getCode()) {
            dao.recover(roleInfo.getId());
            dao.modifyRole(role);
            return true;
        } else {
            throw ServiceExceptionConstants.ROLE_ALREADY_EXIST;
        }
       // return dao.addRole(role);
    }

    @Override
    public boolean delRole(long id)throws Exception {
        if(dao.getDelmark(id) == DeleteMark.DELETED.getCode()){
            throw ServiceExceptionConstants.ROLE_NOT_NOTFOUND;
        }
        return dao.delRole(id);
    }

    @Override
    public boolean modifyRole(RoleSaveInfo role)throws Exception {
        if (StringUtils.isEmpty(role.getDescription())) {
            throw ServiceExceptionConstants.ROLE_NOT_DESCRIPTION;
        }
        if (StringUtils.isEmpty(role.getRolename())) {
            throw ServiceExceptionConstants.ROLE_NOT_NULLRNAME;
        }
        if (null == role.getAuths() || 0 >= role.getAuths().size()) {
            throw ServiceExceptionConstants.ROLE_NOT_NULLRRA;
        }
        if(role.getRoletype()<0){
            throw ServiceExceptionConstants.ROLE_NOT_NULLRTYPE;
        }
        RoleInfo roleInfo = dao.getRoleById(role.getId());
        if (!roleInfo.getRolename().equals(role.getRolename())) {
            if (dao.getRoleByname(role.getRolename()) != null) {
                throw ServiceExceptionConstants.ROLE_ALREADY_ANAMEEXIST;
            }
        }
        return dao.modifyRole(role);
    }

    @Override
    public List<RoleInfo> getAll()throws Exception {
        return dao.getAll();
    }

    @Override
    public RoleCollection search(RoleInfoSearch search) throws Exception{
        return dao.search(search);
    }
}
